﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class NhanVienDAO
    {
        public static DataTable LayTatCaNhanVien()
        {
            SqlConnection connect = null;
            DataTable rs = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }


                rs = SqlHelper.ExecuteDataset(connect, CommandType.StoredProcedure, "usp_LayThongTinNhanVien").Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (rs != null)
                    ((IDisposable)rs).Dispose();

                if (connect != null)
                    connect.Dispose();
            }

            return rs;
        }

        public static NhanVien DangNhap(String user, String pass)
        {
            SqlConnection connect = null;
            NhanVien rs = new NhanVien();

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[2];
                param[0] = new SqlParameter("@user", user);
                param[1] = new SqlParameter("@pass", pass);

                DataTable dt = SqlHelper.ExecuteDataset(connect, CommandType.StoredProcedure, "usp_DangNhap", param).Tables[0];
                if (dt.Rows.Count == 0)
                    return null;
                rs.MaNV = Int32.Parse(dt.Rows[0][0].ToString());
                rs.TenNV = dt.Rows[0][1].ToString();
                rs.Username = dt.Rows[0][2].ToString();
                rs.Password = "";
                rs.ChucVu = dt.Rows[0][4].ToString();

                return rs;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static DataTable LayNhanVienTheoID(int id)
        {
            SqlConnection connect = null;
            NhanVien rs = new NhanVien();

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("@manv", id);

                return SqlHelper.ExecuteDataset(connect, CommandType.StoredProcedure, "usp_LayNhanVienTheoID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }
    }
}
